package com.nd.mobile.study.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.domain.BluebookCatalog;
import com.nd.mobile.study.domain.Subject;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;

public class BlueBookCatalogActivity extends BaseActivity {

	private TextView 		mListTitle;
	
	private ListView				mLV;
	
	private Subject					mSubject;
	
	private BluebookCatalog[]  		mBlueBooks;
	
	private BlueBookCatalogAdapter  mAdapter;
	
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.class_info);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		getBlueBookCatalogList();
	}

	private void getBlueBookCatalogList() {
		mLoading.p();
		new GetBlueBookCatalogTask(this).execute();
	}

	@Override
	protected void initViews() {
		super.initHeader();
		mHeaderTitleTv.setText(R.string.tv_app_courseexam);
		mHeaderBtnHome.setVisibility(View.VISIBLE);
		
		mListTitle = (TextView)findViewById(R.id.tv_list_title);
		mListTitle.setText("选择套卷");
		
		mLV = (ListView) findViewById(R.id.common_lv);
	}

	@Override
	protected void setFieldValues() {
		super.setFieldValues();
		mSubject = (Subject) getIntent().getSerializableExtra(BundleConstants.SUBJECT_INFO);
	}
	private class GetBlueBookCatalogTask extends SimpleAsyncTask<Void, Void, BluebookCatalog[]> {

		public GetBlueBookCatalogTask(Context context) {
			super(context);
		}

		@Override
		protected  BluebookCatalog[] onLoad(Void... params) throws Exception {
			return mSubject.getBluebookCatalog(App.getInstance().getUser());
		}

		@Override
		protected void onUI(BluebookCatalog[] result) {
			mBlueBooks = result;
			mAdapter = new BlueBookCatalogAdapter(mContext, mBlueBooks);
			mLV.setAdapter(mAdapter);
			mLoading.v();
			mLV.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					BluebookCatalog catalog = mBlueBooks[position];
					Intent mIntent = new Intent();
					mIntent.putExtra(BundleConstants.HOMEWORK_INFO, catalog);
					mIntent.setClass(BlueBookCatalogActivity.this, BlueBookInfoDetailActivity.class);
					startActivity(mIntent);
				}
			});
		}

	}

	private class BlueBookCatalogAdapter extends SimpleListAdapter<BluebookCatalog> {

		public BlueBookCatalogAdapter(Context context, BluebookCatalog[] data) {
			super(context, data);
		}

		private class ViewHolder {
			TextView	tvName;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.class_list_item, null);

				holder = new ViewHolder();
				holder.tvName = (TextView) convertView.findViewById(R.id.class_list_tv_name);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvName.setText(mData.get(position).getName());
			return convertView;
		}
	}
	
}
